home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / HARD-PAR.C < prev    next >
C/C++ Source or Header  |  1989-09-20  |  50KB  |  1,717 lines

  1. /* Everything you wanted to know about your machine and C compiler,
  2.    but didn't know who to ask.
  3.    Author: Steven Pemberton, CWI, Amsterdam; steven@cwi.nl
  4.    Bugfixes and upgrades gratefully received.
  5.  
  6.    Name changed to `hard-params' by Richard Stallman, April 89.
  7.    xmalloc function defined, Richard Stallman, June 89.
  8.  
  9.    Copyright (c) 1988, 1989 Steven Pemberton, CWI, Amsterdam.
  10.    All rights reserved.
  11.  
  12.    COMPILING
  13.    With luck and a following wind, just the following will work:
  14.     cc hard-params.c -o hard-params
  15.  
  16.    If your compiler doesn't support:        add flag:
  17.     signed char (eg pcc)            -DNO_SC
  18.     unsigned char                -DNO_UC
  19.     unsigned short and long            -DNO_UI
  20.     signal(), or setjmp/longjmp()        -DNO_SIG
  21.  
  22.    Try it first with no flags, and see if you get any errors - you might be
  23.    surprised. (Most non-ANSI compilers need -DNO_SC, though.)
  24.    Some compilers need a -f flag for floating point.
  25.  
  26.    Don't use any optimisation flags: the program may not work if you do.
  27.    Though "while (a+1.0-a-1.0 == 0.0)" may look like "while(1)" to an
  28.    optimiser, to a floating-point unit there's a world of difference.
  29.  
  30.    Some compilers offer various flags for different floating point
  31.    modes; it's worth trying all possible combinations of these.
  32.  
  33.    Add -DID=\"name\" if you want the machine/flags identified in the output.
  34.  
  35.    SYSTEM DEPENDENCIES
  36.    You may possibly need to add some calls to signal() for other sorts of
  37.    exception on your machine than SIGFPE, and SIGOVER.  See lines beginning
  38.    #ifdef SIGxxx in main() (and communicate the differences to me!).
  39.  
  40.    If your C preprocessor doesn't have the predefined __FILE__ macro, and
  41.    you want to call this file anything other than hard-params.c, change the
  42.    #define command for __FILE__ accordingly.  If it doesn't accept macro
  43.    names at all in #include lines, order a new C compiler. While you're
  44.    waiting for it to arrive, change the last #include in this file (the
  45.    last but one line) accordingly.
  46.  
  47.    OUTPUT
  48.    Run without argument to get the information as English text.  If run
  49.    with argument -l (e.g. hard-params -l), output is a series of #define's for
  50.    the ANSI standard limits.h include file, excluding MB_MAX_CHAR.  If run
  51.    with argument -f, output is a series of #define's for the ANSI standard
  52.    float.h include file.  Flag -v gives verbose output: output includes the
  53.    English text above as C comments.  The program exit(0)'s if everything
  54.    went ok, otherwise it exits with a positive number, telling how many
  55.    problems there were.
  56.  
  57.    VERIFYING THE COMPILER
  58.    If, having produced the float.h and limits.h header files, you want to
  59.    verify that the compiler reads them back correctly (there are a lot of
  60.    boundary cases, of course, like minimum and maximum numbers), you can
  61.    recompile hard-params.c with -DVERIFY set (plus the other flags that you used
  62.    when compiling the version that produced the header files).  This then
  63.    recompiles the program so that it #includes "limits.h" and "float.h",
  64.    and checks that the constants it finds there are the same as the
  65.    constants it produces. Run the resulting program with hard-params -fl.  As of
  66.    this writing, of 21 compiler/flags combinations only 1 compiler has
  67.    passed without error! (The honour goes to 'pcc' on an IBM RT.)
  68.  
  69.    You can also use this option if your compiler already has both files,
  70.    and you want to confirm that this program produces the right results.
  71.  
  72.    TROUBLE SHOOTING.
  73.    This program is now quite trustworthy, and suspicious and wrong output
  74.    may well be caused by bugs in the compiler, not in the program (however
  75.    of course, this is not guaranteed, and no responsibility can be
  76.    accepted, etc.)
  77.  
  78.    The program only works if overflows are ignored by the C system or
  79.    are catchable with signal().
  80.  
  81.    If the program fails to run to completion (often with the error message
  82.    "Unexpected signal at point x"), this often turns out to be a bug in the
  83.    C compiler's run-time system. Check what was about to be printed, and
  84.    try to narrow the problem down.
  85.  
  86.    Another possible problem is that you have compiled the program to produce
  87.    loss-of-precision arithmetic traps. The program cannot cope with these,
  88.    and you should re-compile without them. (They should never be the default).
  89.  
  90.    Make sure you compiled with optimisation turned off.
  91.  
  92.    Output preceded by *** WARNING: identifies behaviour of the C system
  93.    deemed incorrect by the program. Likely problems are that printf or
  94.    scanf don't cope properly with certain boundary numbers.  For each float
  95.    and double that is printed, the printed value is checked that it is
  96.    correct by using sscanf to read it back.  Care is taken that numbers are
  97.    printed with enough digits to uniquely identify them, and therefore that
  98.    they can be read back identically. If the number read back is different,
  99.    the program prints a warning message. If the two numbers in the warning
  100.    look identical, then printf is more than likely rounding the last
  101.    digit(s) incorrectly.  To put you at ease that the two really are
  102.    different, the bit patterns of the two numbers are also printed.  The
  103.    difference is very likely in the last bit.  Many scanf's read the
  104.    minimum double back as 0.0, and similarly cause overflow when reading
  105.    the maximum double.  The program quite ruthlessly declares all these
  106.    behaviours faulty.
  107.  
  108.    The warning that "a cast didn't work" refers to cases like this:
  109.  
  110.       float f;
  111.       #define C 1.234567890123456789
  112.       f= C;
  113.       if (f != (float) C) printf ("Wrong!");
  114.  
  115.    A faulty compiler will widen f to double and ignore the cast to float,
  116.    and because there is more accuracy in a double than a float, fail to
  117.    recognise that they are the same. In the actual case in point, f and C
  118.    are passed as parameters to a function that discovers they are not equal,
  119.    so it's just possible that the error was in the parameter passing,
  120.    not in the cast (see function Validate()).
  121.    For ANSI C, which has float constants, the error message is "constant has
  122.    wrong precision".
  123.  
  124.    REPORTING PROBLEMS
  125.    If the program doesn't work for you for any reason that can't be
  126.    narrowed down to a problem in the C compiler, or it has to be changed in
  127.    order to get it to compile, or it produces suspicious output (like a very
  128.    low maximum float, for instance), please mail the problem and an example
  129.    of the incorrect output to steven@cwi.nl or mcvax!steven.uucp, so that
  130.    improvements can be worked into future versions; mcvax/cwi.nl is the
  131.    European backbone, and is connected to uunet and other fine hosts.
  132.  
  133.    This version of the program is the first to try to catch and diagnose
  134.    bugs in the compiler/run-time system. I would be especially pleased to
  135.    have reports of failures so that I can improve this service.
  136.  
  137.    I apologise unreservedly for the contorted use of the preprocessor...
  138.  
  139.    THE SMALL PRINT
  140.    You may copy and distribute verbatim copies of this source file.
  141.  
  142.    You may modify this source file, and copy and distribute such
  143.    modified versions, provided that you leave the copyright notice
  144.    at the top of the file and also cause the modified file to carry
  145.    prominent notices stating that you changed the files and the date
  146.    of any change; and cause the whole of any work that you distribute
  147.    or publish, that in whole or in part contains or is a derivative of
  148.    this program or any part thereof, to be licensed at no charge to
  149.    all third parties on terms identical to those here.
  150.  
  151.    If you do have a fix to any problem, please send it to me, so that
  152.    other people can have the benefits.
  153.  
  154.    While every effort has been taken to make this program as reliable as
  155.    possible, no responsibility can be taken for the correctness of the
  156.    output, or suitability for any particular use.
  157.  
  158.    ACKNOWLEDGEMENTS
  159.    Many people have given time and ideas to making this program what it is.
  160.    To all of them thanks, and apologies for not mentioning them by name.
  161. */
  162.  
  163. #ifndef __FILE__
  164. #define __FILE__ "hard-params.c"
  165. #endif
  166.  
  167. #ifndef PASS
  168. #define PASS 1
  169. #define PASS1 1
  170. #define VERSION "4.1"
  171.  
  172. /* Procedure just marks the functions that don't return a result */
  173. #ifdef Procedure
  174. #undef Procedure
  175. #endif
  176. #define Procedure
  177.  
  178. #define Vprintf if (V) printf
  179.  
  180. /* stdc is used in tests like if (stdc) */
  181. #ifdef __STDC__
  182. #define stdc 1
  183. #else
  184. #define stdc 0
  185. #endif
  186.  
  187. /* volatile is used to reduce the chance of optimisation,
  188.    and to prevent variables being put in registers (when setjmp/longjmp
  189.    wouldn't work as we want)  */
  190. #ifndef __STDC__
  191. #define volatile static
  192. #endif
  193.  
  194. #include <stdio.h>
  195.  
  196. #ifdef VERIFY
  197. #include "limits.h"
  198. #include "float.h"
  199. #endif
  200.  
  201. #ifdef NO_SIG  /* There's no signal(), or setjmp/longjmp() */
  202.  
  203.     /* Dummy routines instead */
  204.     int lab=1;
  205.     int setjmp(lab) int lab; { return(0); }
  206.     signal(i, p) int i, (*p)(); {}
  207.  
  208. #else
  209.  
  210. #include <signal.h>
  211. #include <setjmp.h>
  212.  
  213.     jmp_buf lab;
  214.     overflow(sig) int sig; { /* what to do on overflow/underflow */
  215.         signal(sig, overflow);
  216.         longjmp(lab, 1);
  217.     }
  218.  
  219. #endif /*NO_SIG*/
  220.  
  221. #define Unexpected(place) if (setjmp(lab)!=0) croak(place)
  222.  
  223. int V= 0,    /* verbose */
  224.     L= 0,    /* produce limits.h */
  225.     F= 0,    /* produce float.h  */
  226.     bugs=0;    /* The number of (possible) bugs in the output */
  227.  
  228. char co[4], oc[4]; /* Comment starter and ender symbols */
  229.  
  230. int bits_per_byte; /* the number of bits per unit returned by sizeof() */
  231.  
  232. #ifdef TEST
  233. /* Set the fp modes on a SUN with 68881 chip, to check that different
  234.    rounding modes etc. get properly detected.
  235.    Compile with additional flag -DTEST, and run with additional parameter
  236.    +hex-number, to set the 68881 mode register to hex-number
  237. */
  238.  
  239. /* Bits 0x30 = rounding mode: */
  240. #define ROUND_BITS    0x30
  241. #define TO_NEAREST    0x00
  242. #define TO_ZERO        0x10
  243. #define TO_MINUS_INF    0x20
  244. #define TO_PLUS_INF    0x30 /* The SUN FP user's guide seems to be wrong here */
  245.  
  246. /* Bits 0xc0 = extended rounding: */
  247. #define EXT_BITS    0xc0
  248. #define ROUND_EXTENDED    0x00
  249. #define ROUND_SINGLE     0x40
  250. #define ROUND_DOUBLE    0x80
  251.  
  252. /* Enabled traps: */
  253. #define EXE_INEX1  0x100
  254. #define EXE_INEX2  0x200
  255. #define EXE_DZ       0x400
  256. #define EXE_UNFL   0x800
  257. #define EXE_OVFL  0x1000
  258. #define EXE_OPERR 0x2000
  259. #define EXE_SNAN  0x4000
  260. #define EXE_BSUN  0x8000
  261.  
  262. printmode(new) unsigned new; {
  263.     fpmode_(&new);
  264.     printf("New fp mode:\n");
  265.     printf("  Round toward ");
  266.     switch (new & ROUND_BITS) {
  267.           case TO_NEAREST:   printf("nearest"); break;
  268.           case TO_ZERO:      printf("zero"); break;
  269.           case TO_MINUS_INF: printf("minus infinity"); break;
  270.           case TO_PLUS_INF:  printf("plus infinity"); break;
  271.           default: printf("???"); break;
  272.     }
  273.  
  274.     printf("\n  Extended rounding precision: ");
  275.  
  276.     switch (new & EXT_BITS) {
  277.           case ROUND_EXTENDED: printf("extended"); break;
  278.           case ROUND_SINGLE:   printf("single"); break;
  279.           case ROUND_DOUBLE:   printf("double"); break;
  280.           default: printf("???"); break;
  281.     }
  282.  
  283.     printf("\n  Enabled exceptions:");
  284.     if (new & (unsigned) EXE_INEX1) printf(" inex1");
  285.     if (new & (unsigned) EXE_INEX2) printf(" inex2");
  286.     if (new & (unsigned) EXE_DZ)    printf(" dz"); 
  287.     if (new & (unsigned) EXE_UNFL)  printf(" unfl"); 
  288.     if (new & (unsigned) EXE_OVFL)  printf(" ovfl"); 
  289.     if (new & (unsigned) EXE_OPERR) printf(" operr"); 
  290.     if (new & (unsigned) EXE_SNAN)  printf(" snan"); 
  291.     if (new & (unsigned) EXE_BSUN)  printf(" bsun"); 
  292.     printf("\n");
  293. }
  294.  
  295. int setmode(s) char *s; {
  296.     unsigned mode=0, dig;
  297.     char c;
  298.  
  299.     while (*s) {
  300.         c= *s++;
  301.         if  (c>='0' && c<='9') dig= c-'0';
  302.         else if (c>='a' && c<='f') dig= c-'a'+10;
  303.         else if (c>='A' && c<='F') dig= c-'A'+10;
  304.         else return 1;
  305.         mode= mode<<4 | dig;
  306.     }
  307.     printmode(mode);
  308.     return 0;
  309. }
  310. #else
  311. int setmode(s) char *s; {
  312.     fprintf(stderr, "Can't set mode: not compiled with TEST\n");
  313.     return(1);
  314. }
  315. #endif
  316.  
  317. croak(place) int place; {
  318.     printf("*** Unexpected signal at point %d\n", place);
  319.     exit(bugs+1); /* An exit isn't essential here, but avoids loops */
  320. }
  321.  
  322. /* This is here in case alloca.c is used.  That wants to call this.  */
  323.  
  324. char *
  325. xmalloc(size) unsigned size; {
  326.     char *malloc();
  327.     char *value = malloc(size);
  328.     if (value == 0) {
  329.         fprintf(stderr, "Virtual memory exceeded\n");
  330.         exit(bugs+1);
  331.     }
  332.     return value;
  333. }
  334.  
  335. main(argc, argv) int argc; char *argv[]; {
  336.     int dprec, fprec, lprec, basic(), fprop(), dprop(), efprop(), edprop();
  337.     char *malloc();
  338.     unsigned int size;
  339.     long total;
  340.     int i; char *s; int bad;
  341.  
  342. #ifdef SIGFPE
  343.     signal(SIGFPE, overflow);
  344. #endif
  345. #ifdef SIGOVER
  346.     signal(SIGOVER, overflow);
  347. #endif
  348. /* Add more calls as necessary */
  349.  
  350.     Unexpected(1);
  351.  
  352.     bad=0;
  353.     for (i=1; i < argc; i++) {
  354.         s= argv[i];
  355.         if (*s == '-') {
  356.             s++;
  357.             while (*s) {
  358.                 switch (*(s++)) {
  359.                       case 'v': V=1; break;
  360.                       case 'l': L=1; break;
  361.                       case 'f': F=1; break;
  362.                       default: bad=1; break;
  363.                 }
  364.             }
  365.         } else if (*s == '+') {
  366.             s++;
  367.             bad= setmode(s);
  368.         } else bad= 1;
  369.     }
  370.     if (bad) {
  371.         fprintf(stderr,
  372.             "Usage: %s [-vlf]\n  v=Verbose l=Limits.h f=Float.h\n",
  373.             argv[0]);
  374.         exit(1);
  375.     }
  376.     if (L || F) {
  377.         co[0]= '/'; oc[0]= ' ';
  378.         co[1]= '*'; oc[1]= '*';
  379.         co[2]= ' '; oc[2]= '/';
  380.         co[3]= '\0'; oc[3]= '\0';
  381.     } else {
  382.         co[0]= '\0'; oc[0]= '\0';
  383.         V=1;
  384.     }
  385.  
  386.     if (L) printf("%slimits.h%s\n", co, oc);
  387.     if (F) printf("%sfloat.h%s\n", co, oc);
  388. #ifdef ID
  389.     printf("%sProduced on %s by hard-params version %s, CWI, Amsterdam%s\n",
  390.            co, ID, VERSION, oc);
  391. #else
  392.     printf("%sProduced by hard-params version %s, CWI, Amsterdam%s\n",
  393.            co, VERSION, oc);
  394. #endif
  395.  
  396. #ifdef VERIFY
  397.     printf("%sVerification phase%s\n", co, oc);
  398. #endif
  399.  
  400. #ifdef NO_SIG
  401.     Vprintf("%sCompiled without signal(): %s%s\n",
  402.         co,
  403.         "there's nothing that can be done if overflow occurs",
  404.         oc);
  405. #endif
  406. #ifdef NO_SC
  407.     Vprintf("%sCompiled without signed char%s\n", co, oc);
  408. #endif
  409. #ifdef NO_UC
  410.     Vprintf("%Compiled without unsigned char%s\n", co, oc);
  411. #endif
  412. #ifdef NO_UI
  413.     Vprintf("%Compiled without unsigned short or long%s\n", co, oc);
  414. #endif
  415. #ifdef __STDC__
  416.     Vprintf("%sCompiler claims to be ANSI C level %d%s\n",
  417.         co, __STDC__, oc);
  418. #else
  419.     Vprintf("%sCompiler does not claim to be ANSI C%s\n", co, oc);
  420. #endif
  421.     printf("\n");
  422.     bits_per_byte= basic();
  423.     Vprintf("\n");
  424.     if (F||V) {
  425.         fprec= fprop(bits_per_byte);
  426.         dprec= dprop(bits_per_byte);
  427.         lprec= ldprop(bits_per_byte);
  428.         efprop(fprec, dprec, lprec);
  429.         edprop(fprec, dprec, lprec);
  430.         eldprop(fprec, dprec, lprec);
  431.     }
  432.     if (V) {
  433.         /* An extra goody: the approximate amount of data-space */
  434.         /* Allocate store until no more available */
  435.         size=1<<((bits_per_byte*sizeof(int))-2);
  436.         total=0;
  437.         while (size!=0) {
  438.             while (malloc(size)!=(char *)NULL) total+=(size/2);
  439.             size/=2;
  440.         }
  441.  
  442.         Vprintf("%sMemory mallocatable ~= %ld Kbytes%s\n",
  443.             co, (total+511)/512, oc);
  444.     }
  445.     exit(bugs);
  446. }
  447.  
  448. Procedure eek_a_bug(problem) char *problem; {
  449.     printf("\n%s*** WARNING: %s%s\n", co, problem, oc);
  450.     bugs++;
  451. }
  452.  
  453. Procedure i_define(sort, name, val, req) char *sort, *name; long val, req; {
  454.     if (val >= 0) {
  455.         printf("#define %s%s %ld\n", sort, name, val);
  456.     } else {
  457.         printf("#define %s%s (%ld)\n", sort, name, val);
  458.     }
  459.     if (val != req) {
  460.         printf("%s*** Verify failed for above #define!\n", co);
  461.         printf("       Compiler has %ld for value%s\n\n", req, oc);
  462.         bugs++;
  463.     }
  464.     Vprintf("\n");
  465. }
  466.  
  467. #ifndef NO_UI
  468.  
  469. #ifdef __STDC__
  470. #define U "U"
  471. #else
  472. #define U ""
  473. #endif
  474.  
  475. Procedure u_define(sort, name, val, req) char *sort, *name; unsigned long val, req; {
  476.     printf("#define %s%s %lu%s\n", sort, name, val, U);
  477.     if (val != req) {
  478.         printf("%s*** Verify failed for above #define!\n", co);
  479.         printf("       Compiler has %lu for value%s\n\n", req, oc);
  480.         bugs++;
  481.     }
  482.     Vprintf("\n");
  483. }
  484. #endif
  485.  
  486. /* Long_double is the longest floating point type available: */
  487. #ifdef __STDC__
  488. #define Long_double long double
  489. #else
  490. #define Long_double double
  491. #endif
  492.  
  493. char *f_rep();
  494.  
  495. Procedure f_define(sort, name, precision, val, mark)
  496.      char *sort, *name; int precision; Long_double val; char *mark; {
  497.     if (stdc) {
  498.         printf("#define %s%s %s%s\n",
  499.                sort, name, f_rep(precision, val), mark);
  500.     } else if (*mark == 'F') {
  501.         /* non-ANSI C has no float constants, so cast the constant */
  502.         printf("#define %s%s ((float)%s)\n",
  503.                sort, name, f_rep(precision, val));
  504.     } else {
  505.         printf("#define %s%s %s\n", sort, name, f_rep(precision, val));
  506.     }
  507.     Vprintf("\n");
  508. }
  509.  
  510. int floor_log(base, x) int base; Long_double x; { /* return floor(log base(x)) */
  511.     int r=0;
  512.     while (x>=base) { r++; x/=base; }
  513.     return r;
  514. }
  515.  
  516. int ceil_log(base, x) int base; Long_double x; {
  517.     int r=0;
  518.     while (x>1.0) { r++; x/=base; }
  519.     return r;
  520. }
  521.  
  522. int exponent(x, fract, exp) Long_double x; double *fract; int *exp; {
  523.     /* Split x into a fraction and a power of ten;
  524.        returns 0 if x is unusable, 1 otherwise.
  525.        Only used for error messages about faulty output.
  526.     */
  527.     int r=0, neg=0;
  528.     Long_double old;
  529.     *fract=0.0; *exp=0;
  530.     if (x<0.0) {
  531.         x= -x;
  532.         neg= 1;
  533.     }
  534.     if (x==0.0) return 1;
  535.     if (x>=10.0) {
  536.         while (x>=10.0) {
  537.             old=x; r++; x/=10.0;
  538.             if (old==x) return 0;
  539.         }
  540.     } else {
  541.         while (x<1.0) {
  542.             old=x; r--; x*=10.0;
  543.             if (old==x) return 0;
  544.         }
  545.     }
  546.     if (neg) *fract= -x;
  547.     else *fract=x;
  548.     *exp=r;
  549.     return 1;
  550. }
  551.  
  552. #define fabs(x) (((x)<0.0)?(-x):(x))
  553.  
  554. char *f_rep(precision, val) int precision; Long_double val; {
  555.     static char buf[1024];
  556.     char *f1;
  557.     if (sizeof(double) == sizeof(Long_double)) {
  558.         /* Assume they're the same, and use non-stdc format */
  559.         /* This is for stdc compilers using non-stdc libraries */
  560.         f1= "%.*e";
  561.     } else {
  562.         /* It had better support Le then */
  563.         f1= "%.*Le";
  564.     }
  565.     sprintf(buf, f1, precision, val);
  566.     return buf;
  567. }
  568.  
  569. Procedure bitpattern(p, size) char *p; int size; {
  570.     char c;
  571.     int i, j;
  572.  
  573.     for (i=1; i<=size; i++) {
  574.         c= *p;
  575.         p++;
  576.         for (j=bits_per_byte-1; j>=0; j--)
  577.             printf("%c", (c>>j)&1 ? '1' : '0');
  578.         if (i!=size) printf(" ");
  579.     }
  580. }
  581.  
  582. #define Order(x, px, mode)\
  583.    printf("%s    %s ", co, mode); for (i=0; i<sizeof(x); i++) px[i]= c[i]; \
  584.    for (i=1; i<=sizeof(x); i++) { putchar((char)((x>>(bits_per_byte*(sizeof(x)-i)))&mask)); }\
  585.    printf("%s\n", oc);
  586.  
  587. Procedure endian(bits_per_byte) int bits_per_byte; {
  588.     /*unsigned*/ short s=0;
  589.     /*unsigned*/ int j=0;
  590.     /*unsigned*/ long l=0;
  591.  
  592.     char *ps= (char *) &s,
  593.          *pj= (char *) &j,
  594.          *pl= (char *) &l,
  595.          *c= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  596.     unsigned int mask, i;
  597.  
  598.     mask=0;
  599.     for (i=1; i<=bits_per_byte; i++) mask= (mask<<1)|1;
  600.  
  601.     if (V) {
  602.         printf("%sCharacter order:%s\n", co, oc);
  603.         Order(s, ps, "short:");
  604.         Order(j, pj, "int:  ");
  605.         Order(l, pl, "long: ");
  606.     }
  607. }
  608.  
  609. #ifdef VERIFY
  610. #ifndef SCHAR_MAX
  611. #define SCHAR_MAX char_max
  612. #define SCHAR_MIN char_min
  613. #endif
  614. #ifndef UCHAR_MAX
  615. #define UCHAR_MAX char_max
  616. #endif
  617. #else
  618. #define CHAR_BIT char_bit
  619. #define CHAR_MAX char_max
  620. #define CHAR_MIN char_min
  621. #define SCHAR_MAX char_max
  622. #define SCHAR_MIN char_min
  623. #define UCHAR_MAX char_max
  624. #endif /* VERIFY */
  625.  
  626. int cprop() { /* Properties of character */
  627.     volatile char c, char_max, char_min;
  628.     volatile int bits_per_byte, is_signed;
  629.     long char_bit;
  630.  
  631.     Unexpected(2);
  632.  
  633.     /* Calculate number of bits per character *************************/
  634.     c=1; bits_per_byte=0;
  635.     do { c=c<<1; bits_per_byte++; } while(c!=0);
  636.     c= (char)(-1);
  637.     if (((int)c)<0) is_signed=1;
  638.     else is_signed=0;
  639.     Vprintf("%sChar = %d bits, %ssigned%s\n",
  640.         co, (int)sizeof(c)*bits_per_byte, (is_signed?"":"un"), oc);
  641.     char_bit=(long)(sizeof(c)*bits_per_byte);
  642.     if (L) i_define("CHAR", "_BIT", char_bit, (long) CHAR_BIT);
  643.  
  644.     c=0; char_max=0;
  645.     c++;
  646.     if (setjmp(lab)==0) { /* Yields char_max */
  647.         while (c>char_max) {
  648.             char_max=c;
  649.             c++;
  650.         }
  651.     } else {
  652.         Vprintf("%sCharacter overflow generates a trap!%s\n", co, oc);
  653.     }
  654.     c=0; char_min=0;
  655.     c--;
  656.     if (setjmp(lab)==0) { /* Yields char_min */
  657.         while (c<char_min) {
  658.             char_min=c;
  659.             c--;
  660.         }
  661.     }
  662.     Unexpected(3);
  663.  
  664.     if (L) {
  665.         i_define("CHAR", "_MAX", (long) char_max, (long) CHAR_MAX);
  666.         i_define("CHAR", "_MIN", (long) char_min, (long) CHAR_MIN);
  667.         if (is_signed) {
  668.             i_define("SCHAR", "_MAX", (long) char_max,
  669.                  (long) SCHAR_MAX);
  670.             i_define("SCHAR", "_MIN", (long) char_min,
  671.                  (long) SCHAR_MIN);
  672.         } else {
  673.             i_define("UCHAR", "_MAX", (long) char_max,
  674.                  (long) UCHAR_MAX);
  675.         }
  676.  
  677.         if (is_signed) {
  678. #ifndef NO_UC
  679.             volatile unsigned char c, char_max;
  680.             c=0; char_max=0;
  681.             c++;
  682.             if (setjmp(lab)==0) { /* Yields char_max */
  683.                 while (c>char_max) {
  684.                     char_max=c;
  685.                     c++;
  686.                 }
  687.             }
  688.             Unexpected(4);
  689.             i_define("UCHAR", "_MAX", (long) char_max,
  690.                  (long) UCHAR_MAX);
  691. #endif
  692.         } else {
  693. #ifndef NO_SC /* Define NO_SC if the next line gives a syntax error */
  694.             volatile signed char c, char_max, char_min;
  695.             c=0; char_max=0;
  696.             c++;
  697.             if (setjmp(lab)==0) { /* Yields char_max */
  698.                 while (c>char_max) {
  699.                     char_max=c;
  700.                     c++;
  701.                 }
  702.             }
  703.             c=0; char_min=0;
  704.             c--;
  705.             if (setjmp(lab)==0) { /* Yields char_min */
  706.                 while (c<char_min) {
  707.                     char_min=c;
  708.                     c--;
  709.                 }
  710.             }
  711.             Unexpected(5);
  712.             i_define("SCHAR", "_MIN", (long) char_min,
  713.                  (long) SCHAR_MIN);
  714.             i_define("SCHAR", "_MAX", (long) char_max,
  715.                  (long) SCHAR_MAX);
  716. #endif /* NO_SC */
  717.         }
  718.     }
  719.     return bits_per_byte;
  720. }
  721.  
  722. int basic() {
  723.     /* The properties of the basic types.
  724.        Returns number of bits per sizeof unit */
  725.     volatile int bits_per_byte;
  726.  
  727.     bits_per_byte= cprop();
  728.  
  729.     /* Shorts, ints and longs *****************************************/
  730.     Vprintf("%sShort=%d int=%d long=%d float=%d double=%d bits %s\n",
  731.         co,
  732.         (int) sizeof(short)*bits_per_byte,
  733.         (int) sizeof(int)*bits_per_byte,
  734.         (int) sizeof(long)*bits_per_byte,
  735.         (int) sizeof(float)*bits_per_byte,
  736.         (int) sizeof(double)*bits_per_byte, oc);
  737.     if (stdc) {
  738.         Vprintf("%sLong double=%d bits%s\n",
  739.             co, (int) sizeof(Long_double)*bits_per_byte, oc);
  740.     }
  741.     Vprintf("%sChar pointers = %d bits%s%s\n",
  742.         co, (int)sizeof(char *)*bits_per_byte,
  743.         sizeof(char *)>sizeof(int)?" BEWARE! larger than int!":"",
  744.         oc);
  745.     Vprintf("%sInt pointers = %d bits%s%s\n",
  746.         co, (int)sizeof(int *)*bits_per_byte,
  747.         sizeof(int *)>sizeof(int)?" BEWARE! larger than int!":"",
  748.         oc);
  749.     sprop();
  750.     iprop();
  751.     lprop();
  752.     usprop();
  753.     uiprop();
  754.     ulprop();
  755.  
  756.     Unexpected(6);
  757.  
  758.     /* Alignment constants ********************************************/
  759.     Vprintf("%sAlignments used for char=%d short=%d int=%d long=%d%s\n",
  760.         co,
  761.         (int)sizeof(struct{char i1; char c1;})-(int)sizeof(char),
  762.         (int)sizeof(struct{short i2; char c2;})-(int)sizeof(short),
  763.         (int)sizeof(struct{int i3; char c3;})-(int)sizeof(int),
  764.         (int)sizeof(struct{long i4; char c4;})-(int)sizeof(long),
  765.         oc);
  766.  
  767.     /* Ten little endians *********************************************/
  768.  
  769.     endian(bits_per_byte);
  770.  
  771.     /* Pointers *******************************************************/
  772.     if (V) {
  773.         if ("abcd"=="abcd")
  774.             printf("%sStrings are shared%s\n", co, oc);
  775.         else printf("%sStrings are not shared%s\n", co, oc);
  776.     }
  777.  
  778.     return bits_per_byte;
  779. }
  780.  
  781. #endif /* ifndef PASS */
  782.  
  783. /* As I said, I apologise for the contortions below. The functions are
  784.    expanded by the preprocessor twice or three times (for float and double,
  785.    and maybe for long double, and for short, int and long). That way,
  786.    I never make a change to one that I forget to make to the other.
  787.    You can look on it as C's fault for not supporting multi-line macro's.
  788.    This whole file is read 3 times by the preprocessor, with PASSn set for
  789.    n=1, 2 or 3, to decide which parts to reprocess.
  790. */
  791.  
  792. /* #undef on an already undefined thing is (wrongly) flagged as an error
  793.    by some compilers, therefore the #ifdef that follows: 
  794. */
  795. #ifdef Number
  796. #undef Number
  797. #undef THING
  798. #undef Thing
  799. #undef thing
  800. #undef FPROP
  801. #undef Fname
  802. #undef Store
  803. #undef Sum
  804. #undef Diff
  805. #undef Mul
  806. #undef Div
  807. #undef Self
  808. #undef F_check
  809. #undef Validate
  810. #undef EPROP
  811. #undef MARK
  812.  
  813. #undef F_RADIX
  814. #undef F_MANT_DIG
  815. #undef F_DIG
  816. #undef F_ROUNDS
  817. #undef F_EPSILON
  818. #undef F_MIN_EXP
  819. #undef F_MIN
  820. #undef F_MIN_10_EXP
  821. #undef F_MAX_EXP
  822. #undef F_MAX
  823. #undef F_MAX_10_EXP
  824. #endif
  825.  
  826. #ifdef Integer
  827. #undef Integer
  828. #undef INT
  829. #undef IPROP
  830. #undef Iname
  831. #undef UPROP
  832. #undef Uname
  833. #undef OK_UI
  834.  
  835. #undef I_MAX
  836. #undef I_MIN
  837. #undef U_MAX
  838. #endif
  839.  
  840. #ifdef PASS1
  841.  
  842. #define Number float
  843. #define THING "FLOAT"
  844. #define Thing "Float"
  845. #define thing "float"
  846. #define Fname "FLT"
  847. #define FPROP fprop
  848. #define Store fStore
  849. #define Sum fSum
  850. #define Diff fDiff
  851. #define Mul fMul
  852. #define Div fDiv
  853. #define Self fSelf
  854. #define F_check fCheck
  855. #define Validate fValidate
  856. #define MARK "F"
  857.  
  858. #define EPROP efprop
  859.  
  860. #define Integer short
  861. #define INT "short"
  862. #define IPROP sprop
  863. #define Iname "SHRT"
  864. #ifndef NO_UI
  865. #define OK_UI 1
  866. #endif
  867.  
  868. #define UPROP usprop
  869. #define Uname "USHRT"
  870.  
  871. #ifdef VERIFY
  872. #define I_MAX SHRT_MAX
  873. #define I_MIN SHRT_MIN
  874. #define U_MAX USHRT_MAX
  875.  
  876. #define F_RADIX FLT_RADIX
  877. #define F_MANT_DIG FLT_MANT_DIG
  878. #define F_DIG FLT_DIG
  879. #define F_ROUNDS FLT_ROUNDS
  880. #define F_EPSILON FLT_EPSILON
  881. #define F_MIN_EXP FLT_MIN_EXP
  882. #define F_MIN FLT_MIN
  883. #define F_MIN_10_EXP FLT_MIN_10_EXP
  884. #define F_MAX_EXP FLT_MAX_EXP
  885. #define F_MAX FLT_MAX
  886. #define F_MAX_10_EXP FLT_MAX_10_EXP
  887. #endif /* VERIFY */
  888.  
  889. #endif /* PASS1 */
  890.  
  891. #ifdef PASS2
  892.  
  893. #define Number double
  894. #define THING "DOUBLE"
  895. #define Thing "Double"
  896. #define thing "double"
  897. #define Fname "DBL"
  898. #define FPROP dprop
  899. #define Store dStore
  900. #define Sum dSum
  901. #define Diff dDiff
  902. #define Mul dMul
  903. #define Div dDiv
  904. #define Self dSelf
  905. #define F_check dCheck
  906. #define Validate dValidate
  907. #define MARK ""
  908.  
  909. #define EPROP edprop
  910.  
  911. #define Integer int
  912. #define INT "int"
  913. #define IPROP iprop
  914. #define Iname "INT"
  915. #define OK_UI 1 /* Unsigned int is always possible */
  916.  
  917. #define UPROP uiprop
  918. #define Uname "UINT"
  919.  
  920. #ifdef VERIFY
  921. #define I_MAX INT_MAX
  922. #define I_MIN INT_MIN
  923. #define U_MAX UINT_MAX
  924.  
  925. #define F_MANT_DIG DBL_MANT_DIG
  926. #define F_DIG DBL_DIG
  927. #define F_EPSILON DBL_EPSILON
  928. #define F_MIN_EXP DBL_MIN_EXP
  929. #define F_MIN DBL_MIN
  930. #define F_MIN_10_EXP DBL_MIN_10_EXP
  931. #define F_MAX_EXP DBL_MAX_EXP
  932. #define F_MAX DBL_MAX
  933. #define F_MAX_10_EXP DBL_MAX_10_EXP
  934. #endif /* VERIFY */
  935.  
  936. #endif /* PASS2 */
  937.  
  938. #ifdef PASS3
  939.  
  940. #ifdef __STDC__
  941. #define Number long double
  942. #endif
  943.  
  944. #define THING "LONG DOUBLE"
  945. #define Thing "Long double"
  946. #define thing "long double"
  947. #define Fname "LDBL"
  948. #define FPROP ldprop
  949. #define Store ldStore
  950. #define Sum ldSum
  951. #define Diff ldDiff
  952. #define Mul ldMul
  953. #define Div ldDiv
  954. #define Self ldSelf
  955. #define F_check ldCheck
  956. #define Validate ldValidate
  957. #define MARK "L"
  958.  
  959. #define EPROP eldprop
  960.  
  961. #define Integer long
  962. #define INT "long"
  963. #define IPROP lprop
  964. #define Iname "LONG"
  965. #ifndef NO_UI
  966. #define OK_UI 1
  967. #endif
  968.  
  969. #define UPROP ulprop
  970. #define Uname "ULONG"
  971.  
  972. #ifdef VERIFY
  973. #define I_MAX LONG_MAX
  974. #define I_MIN LONG_MIN
  975. #define U_MAX ULONG_MAX
  976.  
  977. #define F_MANT_DIG LDBL_MANT_DIG
  978. #define F_DIG LDBL_DIG
  979. #define F_EPSILON LDBL_EPSILON
  980. #define F_MIN_EXP LDBL_MIN_EXP
  981. #define F_MIN LDBL_MIN
  982. #define F_MIN_10_EXP LDBL_MIN_10_EXP
  983. #define F_MAX_EXP LDBL_MAX_EXP
  984. #define F_MAX LDBL_MAX
  985. #define F_MAX_10_EXP LDBL_MAX_10_EXP
  986. #endif /* VERIFY */
  987.  
  988. #endif /* PASS3 */
  989.  
  990. #ifndef VERIFY
  991. #define I_MAX int_max
  992. #define I_MIN int_min
  993. #define U_MAX int_max
  994.  
  995. #define F_RADIX f_radix
  996. #define F_MANT_DIG f_mant_dig
  997. #define F_DIG f_dig
  998. #define F_ROUNDS f_rounds
  999. #define F_EPSILON f_epsilon
  1000. #define F_MIN_EXP f_min_exp
  1001. #define F_MIN f_min
  1002. #define F_MIN_10_EXP f_min_10_exp
  1003. #define F_MAX_EXP f_max_exp
  1004. #define F_MAX f_max
  1005. #define F_MAX_10_EXP f_max_10_exp
  1006. #endif
  1007.  
  1008. Procedure IPROP() { /* for short, int, and long */
  1009.     volatile Integer newi, int_max, maxeri, int_min, minneri;
  1010.     volatile int ibits, ipower, two=2;
  1011.  
  1012.     /* Calculate max short/int/long ***********************************/
  1013.     /* Calculate 2**n-1 until overflow - then use the previous value  */
  1014.  
  1015.     newi=1; int_max=0;
  1016.  
  1017.     if (setjmp(lab)==0) { /* Yields int_max */
  1018.         for(ipower=0; newi>int_max; ipower++) {
  1019.             int_max=newi;
  1020.             newi=newi*two+1;
  1021.         }
  1022.         Vprintf("%sOverflow of a%s %s does not generate a trap%s\n",
  1023.             co, INT[0]=='i'?"n":"", INT, oc);
  1024.     } else {
  1025.         Vprintf("%sOverflow of a%s %s generates a trap%s\n",
  1026.             co, INT[0]=='i'?"n":"", INT, oc);
  1027.     }
  1028.     Unexpected(7);
  1029.  
  1030.     /* Minimum value: assume either two's or one's complement *********/
  1031.     int_min= -int_max;
  1032.     if (setjmp(lab)==0) { /* Yields int_min */
  1033.         if (int_min-1 < int_min) int_min--;
  1034.     }
  1035.     Unexpected(8);
  1036.  
  1037.     /* Now for those daft Cybers: */
  1038.  
  1039.     maxeri=0; newi=int_max;
  1040.  
  1041.     if (setjmp(lab)==0) { /* Yields maxeri */
  1042.         for(ibits=ipower; newi>maxeri; ibits++) {
  1043.             maxeri=newi;
  1044.             newi=newi+newi+1;
  1045.         }
  1046.     }
  1047.     Unexpected(9);
  1048.  
  1049.     minneri= -maxeri;
  1050.     if (setjmp(lab)==0) { /* Yields minneri */
  1051.         if (minneri-1 < minneri) minneri--;
  1052.     }
  1053.     Unexpected(10);
  1054.  
  1055.     Vprintf("%sMaximum %s = %ld (= 2**%d-1)%s\n",
  1056.         co, INT, (long)int_max, ipower, oc);
  1057.     Vprintf("%sMinimum %s = %ld%s\n", co, INT, (long)int_min, oc);
  1058.  
  1059.     if (L) i_define(Iname, "_MAX", (long) int_max, (long) I_MAX);
  1060.     if (L) i_define(Iname, "_MIN", (long) int_min, (long) I_MIN);
  1061.  
  1062.     if (maxeri>int_max) {
  1063.         Vprintf("%sThere is a larger %s, %ld (= 2**%d-1), %s %s%s\n",
  1064.             co, INT, (long)maxeri, ibits, 
  1065.             "but only for addition, not multiplication",
  1066.             "(I smell a Cyber!)",
  1067.             oc);
  1068.     }
  1069.  
  1070.     if (minneri<int_min) {
  1071.         Vprintf("%sThere is a smaller %s, %ld, %s %s%s\n",
  1072.             co, INT, (long)minneri, 
  1073.             "but only for addition, not multiplication",
  1074.             "(I smell a Cyber!)",
  1075.             oc);
  1076.     }
  1077. }
  1078.  
  1079. Procedure UPROP () { /* unsigned short/int/long */
  1080. #ifdef OK_UI
  1081.     volatile unsigned Integer int_max, newi, two;
  1082.     newi=1; int_max=0; two=2;
  1083.  
  1084.     if (setjmp(lab)==0) { /* Yields int_max */
  1085.         while(newi>int_max) {
  1086.             int_max=newi;
  1087.             newi=newi*two+1;
  1088.         }
  1089.     }
  1090.     Unexpected(11);
  1091.     Vprintf("%sMaximum unsigned %s = %lu%s\n",
  1092.         co, INT, (unsigned long) int_max, oc);
  1093.     if (L) u_define(Uname, "_MAX", (unsigned long) int_max,
  1094.             (unsigned long) U_MAX);
  1095. #endif
  1096. }
  1097.  
  1098.  
  1099. #ifdef Number
  1100.  
  1101. /* These routines are intended to defeat any attempt at optimisation
  1102.    or use of extended precision, and to defeat faulty narrowing casts:
  1103. */
  1104. Procedure Store(a, b) Number a, *b; { *b=a; }
  1105. Number Sum(a, b) Number a, b; { Number r; Store(a+b, &r); return (r); }
  1106. Number Diff(a, b) Number a, b; { Number r; Store(a-b, &r); return (r); }
  1107. Number Mul(a, b) Number a, b; { Number r; Store(a*b, &r); return (r); }
  1108. Number Div(a, b) Number a, b; { Number r; Store(a/b, &r); return (r); }
  1109. Number Self(a) Number a; { Number r; Store(a, &r); return (r); }
  1110.  
  1111. Procedure F_check(precision, val1) int precision; Long_double val1; {
  1112.     /* You don't think I'm going to go to all the trouble of writing
  1113.        a program that works out what all sorts of values are, only to
  1114.        have printf go and print the wrong values out, do you?
  1115.        No, you're right, so this function tries to see if printf
  1116.        has written the right value, by reading it back again.
  1117.        This introduces a new problem of course: suppose printf writes
  1118.        the correct value, and scanf reads it back wrong... oh well.
  1119.        But I'm adamant about this: the precision given is enough
  1120.        to uniquely identify the printed number, therefore I insist
  1121.        that sscanf read the number back identically. Harsh yes, but
  1122.        sometimes you've got to be cruel to be kind.
  1123.     */
  1124.     Long_double new1;
  1125.     Number val, new, diff;
  1126.     double rem;
  1127.     int e;
  1128.     char *rep;
  1129.     char *f2;
  1130.  
  1131.     if (sizeof(double) == sizeof(Long_double)) {
  1132.         /* Assume they're the same, and use non-stdc format */
  1133.         /* This is for stdc compilers using non-stdc libraries */
  1134.         f2= "%le";   /* Input */
  1135.     } else {
  1136.         /* It had better support Le then */
  1137.         f2= "%Le";
  1138.     }
  1139.     val= val1;
  1140.     rep= f_rep(precision, (Long_double) val);
  1141.     if (setjmp(lab)==0) {
  1142.         sscanf(rep, f2, &new1);
  1143.     } else {
  1144.         eek_a_bug("sscanf caused a trap");
  1145.         printf("%s    scanning: %s format: %s%s\n\n", co, rep, f2, oc);
  1146.         Unexpected(12);
  1147.         return;
  1148.     }
  1149.  
  1150.     if (setjmp(lab)==0) { /* See if new is usable */
  1151.         new= new1;
  1152.         if (new != 0.0) {
  1153.             diff= val/new - 1.0;
  1154.             if (diff < 0.1) diff= 1.0;
  1155.             /* That should be enough to generate a trap */
  1156.         }
  1157.     } else {
  1158.         eek_a_bug("sscanf returned an unusable number");
  1159.         printf("%s    scanning: %s with format: %s%s\n\n",
  1160.                co, rep, f2, oc);
  1161.         Unexpected(13);
  1162.         return;
  1163.     }
  1164.  
  1165.     Unexpected(14);
  1166.     if (new != val) {
  1167.         eek_a_bug("Possibly bad output from printf above");
  1168.         if (!exponent(val, &rem, &e)) {
  1169.             printf("%s    but value was an unusable number%s\n\n",
  1170.                    co, oc);
  1171.             return;
  1172.         }
  1173.         printf("%s    expected value around %.*fe%d, bit pattern:\n    ",
  1174.                co, precision, rem, e);
  1175.         bitpattern((char *) &val, sizeof(val));
  1176.         printf ("%s\n", oc);
  1177.         printf("%s    sscanf gave           %s, bit pattern:\n    ",
  1178.                co, f_rep(precision, (Long_double) new));
  1179.         bitpattern((char *) &new, sizeof(new));
  1180.         printf ("%s\n", oc);
  1181.         printf("%s    difference= %s%s\n\n", 
  1182.                co, f_rep(precision, (Long_double) (val-new)), oc);
  1183.     }
  1184. }
  1185.  
  1186. Procedure Validate(prec, val, req, same) int prec, same; Long_double val, req; {
  1187.     Unexpected(15);
  1188.     if (!same) {
  1189.         printf("%s*** Verify failed for above #define!\n", co);
  1190.         if (setjmp(lab) == 0) { /* for the case that req == nan */
  1191.             printf("       Compiler has %s for value%s\n", 
  1192.                    f_rep(prec, req), oc);
  1193.         } else {
  1194.             printf("       Compiler has %s for value%s\n",
  1195.                    "an unusable number", oc);
  1196.         }
  1197.         if (setjmp(lab) == 0) {
  1198.             F_check(prec, (Long_double) req);
  1199.         } /*else forget it*/
  1200.         if (setjmp(lab) == 0) {        
  1201.             if (req > 0.0 && val > 0.0) {
  1202.                 printf("%s    difference= %s%s\n",
  1203.                        co, f_rep(prec, val-req), oc);
  1204.             }
  1205.         } /*else forget it*/
  1206.         Unexpected(16);
  1207.         printf("\n");
  1208.         bugs++;
  1209.     } else if (val != req) {
  1210.         if (stdc) {
  1211.             printf("%s*** Verify failed for above #define!\n", co);
  1212.             printf("       Constant has the wrong precision%s\n",
  1213.                    oc);
  1214.             bugs++;
  1215.         } else eek_a_bug("the cast didn't work");
  1216.         printf("\n");
  1217.     }
  1218. }
  1219.  
  1220. int FPROP(bits_per_byte) int bits_per_byte; {
  1221.     /* Properties of floating types, using algorithms by Cody and Waite
  1222.        from MA Malcolm, as modified by WM Gentleman and SB Marovich.
  1223.        Further extended by S Pemberton.
  1224.  
  1225.        Returns the number of digits in the fraction.
  1226.     */
  1227.  
  1228.     volatile int i, f_radix, iexp, irnd, mrnd, f_rounds, f_mant_dig,
  1229.         iz, k, inf, machep, f_max_exp, f_min_exp, mx, negeps,
  1230.         mantbits, digs, f_dig, trap,
  1231.         hidden, normal, f_min_10_exp, f_max_10_exp;
  1232.     volatile Number a, b, base, basein, basem1, f_epsilon, epsneg,
  1233.            f_max, newxmax, f_min, xminner, y, y1, z, z1, z2;
  1234.  
  1235.     Unexpected(17);
  1236.  
  1237.     Vprintf("%sPROPERTIES OF %s:%s\n", co, THING, oc);
  1238.  
  1239.     /* Base and size of mantissa **************************************/
  1240.     /* First repeatedly double until adding 1 has no effect.      */
  1241.     /* For instance, if base is 10, with 3 significant digits      */
  1242.     /* it will try 1, 2, 4, 8, ... 512, 1024, and stop there,      */
  1243.     /* since 1024 is only representable as 1020.              */
  1244.     a=1.0;
  1245.     if (setjmp(lab)==0) { /* inexact trap? */
  1246.         do { a=Sum(a, a); }
  1247.         while (Diff(Diff(Sum(a, 1.0), a), 1.0) == 0.0);
  1248.     } else {
  1249.         fprintf(stderr, "*** Program got loss-of-precision trap!\n");
  1250.         /* And supporting those is just TOO much trouble! */
  1251.         exit(bugs+1);
  1252.     }
  1253.     Unexpected(18);
  1254.     /* Now double until you find a number that can be added to the      */
  1255.     /* above number. For 1020 this is 8 or 16, depending whether the  */
  1256.     /* result is rounded or truncated.                  */
  1257.     /* In either case the result is 1030. 1030-1020= the base, 10.      */
  1258.     b=1.0;
  1259.     do { b=Sum(b, b); } while ((base=Diff(Sum(a, b), a)) == 0.0);
  1260.     f_radix=base;
  1261.     Vprintf("%sBase = %d%s\n", co, f_radix, oc);
  1262.  
  1263.     /* Sanity check; if base<2, I can't guarantee the rest will work  */
  1264.     if (f_radix < 2) {
  1265.         eek_a_bug("Function return or parameter passing faulty? (This is a guess.)");
  1266.         printf("\n");
  1267.         return(0);
  1268.     }
  1269.  
  1270. #ifdef PASS1 /* only for FLT */
  1271.     if (F) i_define("FLT", "_RADIX", (long) f_radix, (long) F_RADIX);
  1272. #endif
  1273.  
  1274.     /* Now the number of digits precision: */
  1275.     f_mant_dig=0; b=1.0;
  1276.     do { f_mant_dig++; b=Mul(b, base); }
  1277.     while (Diff(Diff(Sum(b,1.0),b),1.0) == 0.0);
  1278.     f_dig=floor_log(10, (Long_double)(b/base)) + (base==10?1:0);
  1279.     Vprintf("%sSignificant base digits = %d %s %d %s%s\n",
  1280.         co, f_mant_dig, "(= at least", f_dig, "decimal digits)", oc);
  1281.     if (F) i_define(Fname, "_MANT_DIG", (long) f_mant_dig,
  1282.             (long) F_MANT_DIG);
  1283.     if (F) i_define(Fname, "_DIG", (long) f_dig, (long) F_DIG);
  1284.     digs= ceil_log(10, (Long_double)b); /* the number of digits to printf */
  1285.  
  1286.     /* Rounding *******************************************************/
  1287.     basem1=Diff(base, 0.5);
  1288.     if (Diff(Sum(a, basem1), a) != 0.0) {
  1289.         if (f_radix == 2) basem1=0.375;
  1290.         else basem1=1.0;
  1291.         if (Diff(Sum(a, basem1), a) != 0.0) irnd=2; /* away from 0 */
  1292.         else irnd=1; /* to nearest */
  1293.     } else irnd=0; /* towards 0 */
  1294.  
  1295.     basem1=Diff(base, 0.5);
  1296.  
  1297.     if (Diff(Diff(-a, basem1), -a) != 0.0) {
  1298.         if (f_radix == 2) basem1=0.375;
  1299.         else basem1=1.0;
  1300.         if (Diff(Diff(-a, basem1), -a) != 0.0) mrnd=2; /* away from 0*/
  1301.         else mrnd=1; /* to nearest */
  1302.     } else mrnd=0; /* towards 0 */
  1303.  
  1304.     f_rounds=4; /* Unknown rounding */
  1305.     if (irnd==0 && mrnd==0) f_rounds=0; /* zero = chops */
  1306.     if (irnd==1 && mrnd==1) f_rounds=1; /* nearest */
  1307.     if (irnd==2 && mrnd==0) f_rounds=2; /* +inf */
  1308.     if (irnd==0 && mrnd==2) f_rounds=3; /* -inf */
  1309.  
  1310.     if (f_rounds != 4) {
  1311.         Vprintf("%sArithmetic rounds towards ", co);
  1312.         switch (f_rounds) {
  1313.               case 0: Vprintf("zero (i.e. it chops)"); break;
  1314.               case 1: Vprintf("nearest"); break;
  1315.               case 2: Vprintf("+infinity"); break;
  1316.               case 3: Vprintf("-infinity"); break;
  1317.               default: Vprintf("???"); break;
  1318.         }
  1319.         Vprintf("%s\n", oc);
  1320.     } else { /* Hmm, try to give some help here: */
  1321.         Vprintf("%sArithmetic rounds oddly: %s\n", co, oc);
  1322.         Vprintf("%s    Negative numbers %s%s\n",
  1323.             co, mrnd==0 ? "towards zero" :
  1324.                 mrnd==1 ? "to nearest" :
  1325.                       "away from zero",
  1326.             oc);
  1327.         Vprintf("%s    Positive numbers %s%s\n",
  1328.             co, irnd==0 ? "towards zero" :
  1329.                 irnd==1 ? "to nearest" :
  1330.                       "away from zero",
  1331.             oc);
  1332.     }
  1333.     /* An extra goody */
  1334.     if (f_radix == 2 && f_rounds == 1) {
  1335.         if (Diff(Sum(a, 1.0), a) != 0.0) {
  1336.             Vprintf("%s   Tie breaking rounds up%s\n", co, oc);
  1337.         } else if (Diff(Sum(a, 3.0), a) == 4.0) {
  1338.             Vprintf("%s   Tie breaking rounds to even%s\n", co, oc);
  1339.         } else {
  1340.             Vprintf("%s   Tie breaking rounds down%s\n", co, oc);
  1341.         }
  1342.     }
  1343. #ifdef PASS1 /* only for FLT */
  1344.     if (F) i_define("FLT", "_ROUNDS", (long) f_rounds, (long) F_ROUNDS);
  1345. #endif
  1346.  
  1347.     /* Various flavours of epsilon ************************************/
  1348.     negeps=f_mant_dig+f_mant_dig;
  1349.     basein=1.0/base;
  1350.     a=1.0;
  1351.     for(i=1; i<=negeps; i++) a*=basein;
  1352.  
  1353.     b=a;
  1354.     while (Diff(Diff(1.0, a), 1.0) == 0.0) {
  1355.         a*=base;
  1356.         negeps--;
  1357.     }
  1358.     negeps= -negeps;
  1359.     Vprintf("%sSmallest x such that 1.0-base**x != 1.0 = %d%s\n",
  1360.         co, negeps, oc);
  1361.  
  1362.     epsneg=a;
  1363.     if ((f_radix!=2) && irnd) {
  1364.     /*    a=(a*(1.0+a))/(1.0+1.0); => */
  1365.         a=Div(Mul(a, Sum(1.0, a)), Sum(1.0, 1.0));
  1366.     /*    if ((1.0-a)-1.0 != 0.0) epsneg=a; => */
  1367.         if (Diff(Diff(1.0, a), 1.0) != 0.0) epsneg=a;
  1368.     }
  1369.     Vprintf("%sSmall x such that 1.0-x != 1.0 = %s%s\n",
  1370.         co, f_rep(digs, (Long_double) epsneg), oc);
  1371.     /* it may not be the smallest */
  1372.     if (V) F_check(digs, (Long_double) epsneg);
  1373.     Unexpected(19);
  1374.  
  1375.     machep= -f_mant_dig-f_mant_dig;
  1376.     a=b;
  1377.     while (Diff(Sum(1.0, a), 1.0) == 0.0) { a*=base; machep++; }
  1378.     Vprintf("%sSmallest x such that 1.0+base**x != 1.0 = %d%s\n",
  1379.         co, machep, oc);
  1380.  
  1381.     f_epsilon=a;
  1382.     if ((f_radix!=2) && irnd) {
  1383.     /*    a=(a*(1.0+a))/(1.0+1.0); => */
  1384.         a=Div(Mul(a, Sum(1.0, a)), Sum(1.0, 1.0));
  1385.     /*    if ((1.0+a)-1.0 != 0.0) f_epsilon=a; => */
  1386.         if (Diff(Sum(1.0, a), 1.0) != 0.0) f_epsilon=a;
  1387.     }
  1388.     Vprintf("%sSmallest x such that 1.0+x != 1.0 = %s%s\n",
  1389.         co, f_rep(digs, (Long_double) f_epsilon), oc);
  1390.     /* Possible loss of precision warnings here from non-stdc compilers: */
  1391.     if (F) f_define(Fname, "_EPSILON", digs, (Long_double) f_epsilon, MARK);
  1392.     if (V || F) F_check(digs, (Long_double) f_epsilon);
  1393.     Unexpected(20);
  1394.     if (F) Validate(digs, (Long_double) f_epsilon, (Long_double) F_EPSILON,
  1395.             f_epsilon == Self(F_EPSILON));
  1396.     Unexpected(21);
  1397.  
  1398.     /* Extra chop info *************************************************/
  1399.     if (f_rounds == 0) {
  1400.         if (Diff(Mul(Sum(1.0,f_epsilon),1.0),1.0) !=  0.0) {
  1401.             Vprintf("%sAlthough arithmetic chops, it uses guard digits%s\n", co, oc);
  1402.         }
  1403.     }
  1404.  
  1405.     /* Size of and minimum normalised exponent ************************/
  1406.     y=0; i=0; k=1; z=basein; z1=(1.0+f_epsilon)/base;
  1407.  
  1408.     /* Coarse search for the largest power of two */
  1409.     if (setjmp(lab)==0) { /* for underflow trap */ /* Yields i, k, y, y1 */
  1410.         do {
  1411.             y=z; y1=z1;
  1412.             z=Mul(y,y); z1=Mul(z1, y);
  1413.             a=Mul(z,1.0);
  1414.             z2=Div(z1,y);
  1415.             if (z2 != y1) break;
  1416.             if ((Sum(a,a) == 0.0) || (fabs(z) >= y)) break;
  1417.             i++;
  1418.             k+=k;
  1419.         } while(1);
  1420.     } else {
  1421.         Vprintf("%s%s underflow generates a trap%s\n", co, Thing, oc);
  1422.     }
  1423.     Unexpected(22);
  1424.  
  1425.     if (f_radix != 10) {
  1426.         iexp=i+1; /* for the sign */
  1427.         mx=k+k;
  1428.     } else {
  1429.         iexp=2;
  1430.         iz=f_radix;
  1431.         while (k >= iz) { iz*=f_radix; iexp++; }
  1432.         mx=iz+iz-1;
  1433.     }
  1434.  
  1435.     /* Fine tune starting with y and y1 */
  1436.     if (setjmp(lab)==0) { /* for underflow trap */ /* Yields k, f_min */
  1437.         do {
  1438.             f_min=y; z1=y1;
  1439.             y=Div(y,base); y1=Div(y1,base);
  1440.             a=Mul(y,1.0);
  1441.             z2=Mul(y1,base);
  1442.             if (z2 != z1) break;
  1443.             if ((Sum(a,a) == 0.0) || (fabs(y) >= f_min)) break;
  1444.             k++;
  1445.         } while (1);
  1446.     }
  1447.     Unexpected(23);
  1448.  
  1449.     f_min_exp=(-k)+1;
  1450.  
  1451.     if ((mx <= k+k-3) && (f_radix != 10)) { mx+=mx; iexp+=1; }
  1452.     Vprintf("%sNumber of bits used for exponent = %d%s\n", co, iexp, oc);
  1453.     Vprintf("%sMinimum normalised exponent = %d%s\n", co, f_min_exp, oc);
  1454.     if (F) i_define(Fname, "_MIN_EXP", (long) f_min_exp, (long) F_MIN_EXP);
  1455.  
  1456.     if (setjmp(lab)==0) {
  1457.         Vprintf("%sMinimum normalised positive number = %s%s\n",
  1458.             co, f_rep(digs, (Long_double) f_min), oc);
  1459.     } else {
  1460.         eek_a_bug("printf can't print the smallest normalised number");
  1461.         printf("\n");
  1462.     }
  1463.     Unexpected(24);
  1464.     /* Possible loss of precision warnings here from non-stdc compilers: */
  1465.     if (setjmp(lab) == 0) {
  1466.         if (F) f_define(Fname, "_MIN", digs, (Long_double) f_min, MARK);
  1467.         if (V || F) F_check(digs, (Long_double) f_min);
  1468.     } else {
  1469.         eek_a_bug("xxx_MIN caused a trap");
  1470.         printf("\n");
  1471.     }
  1472.  
  1473.     if (setjmp(lab) == 0) {
  1474.         if (F) Validate(digs, (Long_double) f_min, (Long_double) F_MIN,
  1475.                 f_min == Self(F_MIN));
  1476.     } else {
  1477.         printf("%s*** Verify failed for above #define!\n    %s %s\n\n",
  1478.                co, "Compiler has an unusable number for value", oc);
  1479.         bugs++;
  1480.     }
  1481.     Unexpected(25);
  1482.  
  1483.     a=1.0; f_min_10_exp=0;
  1484.     while (a > f_min*10.0) { a/=10.0; f_min_10_exp--; }
  1485.     if (F) i_define(Fname, "_MIN_10_EXP", (long) f_min_10_exp,
  1486.             (long) F_MIN_10_EXP);
  1487.  
  1488.     /* Minimum exponent ************************************************/
  1489.     if (setjmp(lab)==0) { /* for underflow trap */ /* Yields xminner */
  1490.         do {
  1491.             xminner=y;
  1492.             y=Div(y,base);
  1493.             a=Mul(y,1.0);
  1494.             if ((Sum(a,a) == 0.0) || (fabs(y) >= xminner)) break;
  1495.         } while (1);
  1496.     }
  1497.     Unexpected(26);
  1498.  
  1499.     if (xminner != 0.0 && xminner != f_min) {
  1500.         normal= 0;
  1501.         Vprintf("%sThe smallest numbers are not kept normalised%s\n",
  1502.             co, oc);
  1503.         if (setjmp(lab)==0) {
  1504.             Vprintf("%sSmallest unnormalised positive number = %s%s\n",
  1505.                 co, f_rep(digs, (Long_double) xminner), oc);
  1506.             if (V) F_check(digs, (Long_double) xminner);
  1507.         } else {
  1508.             eek_a_bug("printf can't print the smallest unnormalised number.");
  1509.             printf("\n");
  1510.         }
  1511.         Unexpected(27);
  1512.     } else {
  1513.         normal= 1;
  1514.         Vprintf("%sThe smallest numbers are normalised%s\n", co, oc);
  1515.     }
  1516.  
  1517.     /* Maximum exponent ************************************************/
  1518.     f_max_exp=2; f_max=1.0; newxmax=base+1.0;
  1519.     inf=0; trap=0;
  1520.     while (f_max<newxmax) {
  1521.         f_max=newxmax;
  1522.         if (setjmp(lab) == 0) { /* Yields inf, f_max_exp */
  1523.             newxmax=Mul(newxmax, base);
  1524.         } else {
  1525.             trap=1;
  1526.             break;
  1527.         }
  1528.         if (Div(newxmax, base) != f_max) {
  1529.             inf=1; /* ieee infinity */
  1530.             break;
  1531.         }
  1532.         f_max_exp++;
  1533.     }
  1534.     Unexpected(28);
  1535.     if (trap) {
  1536.         Vprintf("%s%s overflow generates a trap%s\n", co, Thing, oc);
  1537.     }
  1538.  
  1539.     if (inf) Vprintf("%sThere is an 'infinite' value%s\n", co, oc);
  1540.     Vprintf("%sMaximum exponent = %d%s\n", co, f_max_exp, oc);
  1541.     if (F) i_define(Fname, "_MAX_EXP", (long) f_max_exp, (long) F_MAX_EXP);
  1542.  
  1543.     /* Largest number ***************************************************/
  1544.     f_max=Diff(1.0, epsneg);
  1545.     if (Mul(f_max,1.0) != f_max) f_max=Diff(1.0, Mul(base,epsneg));
  1546.     for (i=1; i<=f_max_exp; i++) f_max=Mul(f_max, base);
  1547.  
  1548.     if (setjmp(lab)==0) {
  1549.         Vprintf("%sMaximum number = %s%s\n",
  1550.             co, f_rep(digs, (Long_double) f_max), oc);
  1551.     } else {
  1552.         eek_a_bug("printf can't print the largest double.");
  1553.         printf("\n");
  1554.     }
  1555.     if (setjmp(lab)==0) {
  1556.     /* Possible loss of precision warnings here from non-stdc compilers: */
  1557.         if (F) f_define(Fname, "_MAX", digs, (Long_double) f_max, MARK);
  1558.         if (V || F) F_check(digs, (Long_double) f_max);
  1559.     } else {
  1560.         eek_a_bug("xxx_MAX caused a trap");
  1561.         printf("\n");
  1562.     }
  1563.     if (setjmp(lab)==0) {
  1564.         if (F) Validate(digs, (Long_double) f_max, (Long_double) F_MAX,
  1565.                 f_max == Self(F_MAX));
  1566.     } else {
  1567.         printf("%s*** Verify failed for above #define!\n    %s %s\n\n",
  1568.                co, "Compiler has an unusable number for value", oc);
  1569.         bugs++;
  1570.     }
  1571.     Unexpected(29);
  1572.  
  1573.     a=1.0; f_max_10_exp=0;
  1574.     while (a < f_max/10.0) { a*=10.0; f_max_10_exp++; }
  1575.     if (F) i_define(Fname, "_MAX_10_EXP", (long) f_max_10_exp,
  1576.             (long) F_MAX_10_EXP);
  1577.  
  1578.     /* Hidden bit + sanity check ****************************************/
  1579.     if (f_radix != 10) {
  1580.         hidden=0;
  1581.         mantbits=floor_log(2, (Long_double)f_radix)*f_mant_dig;
  1582.         if (mantbits+iexp == (int)sizeof(Number)*bits_per_byte) {
  1583.             hidden=1;
  1584.             Vprintf("%sArithmetic uses a hidden bit%s\n", co, oc);
  1585.         } else if (mantbits+iexp+1 == (int)sizeof(Number)*bits_per_byte) {
  1586.             Vprintf("%sArithmetic doesn't use a hidden bit%s\n",
  1587.                 co, oc);
  1588.         } else {
  1589.             printf("\n%s%s\n    %s %s %s!%s\n\n",
  1590.                    co,
  1591.                    "*** Something fishy here!",
  1592.                    "Exponent size + mantissa size doesn't match",
  1593.                    "with the size of a", thing,
  1594.                    oc);
  1595.         }
  1596.         if (hidden && f_radix == 2 && f_max_exp+f_min_exp==3) {
  1597.             Vprintf("%sIt looks like %s length IEEE format%s\n",
  1598.                 co, f_mant_dig==24 ? "single" :
  1599.                     f_mant_dig==53 ? "double" :
  1600.                     f_mant_dig >53 ? "extended" :
  1601.                         "some", oc);
  1602.             if (f_rounds != 1 || normal) {
  1603.                 Vprintf("%s   though ", co);
  1604.                 if (f_rounds != 1) {
  1605.                     Vprintf("the rounding is unusual");
  1606.                     if (normal) Vprintf(" and ");
  1607.                 }
  1608.                 if (normal) Vprintf("the normalisation is unusual");
  1609.                 Vprintf("%s\n", oc);
  1610.             }
  1611.         } else {
  1612.             Vprintf("%sIt doesn't look like IEEE format%s\n",
  1613.                 co, oc);
  1614.         }
  1615.     }
  1616.     printf("\n"); /* regardless of verbosity */
  1617.     return f_mant_dig;
  1618. }
  1619.  
  1620. Procedure EPROP(fprec, dprec, lprec) int fprec, dprec, lprec; {
  1621.     /* See if expressions are evaluated in extended precision.
  1622.        Some compilers optimise even if you don't want it,
  1623.        and then this function fails to produce the right result.
  1624.        We try to diagnose this if it happens.
  1625.     */
  1626.     volatile int eprec;
  1627.     volatile double a, b, base, old;
  1628.     volatile Number d, oldd, dbase, one, zero;
  1629.     volatile int bad=0;
  1630.  
  1631.     /* Size of mantissa **************************************/
  1632.     a=1.0;
  1633.     if (setjmp(lab) == 0) { /* Yields nothing */
  1634.         do { old=a; a=a+a; }
  1635.         while ((((a+1.0)-a)-1.0) == 0.0 && a>old);
  1636.     } else bad=1;
  1637.     if (a <= old) bad=1;
  1638.  
  1639.     if (!bad) {
  1640.         b=1.0;
  1641.         if (setjmp(lab) == 0) { /* Yields nothing */
  1642.             do { old=b; b=b+b; }
  1643.             while ((base=((a+b)-a)) == 0.0 && b>old);
  1644.             if (b <= old) bad=1;
  1645.         } else bad=1;
  1646.     }
  1647.  
  1648.     if (!bad) {
  1649.         eprec=0; d=1.0; dbase=base; one=1.0; zero=0.0;
  1650.         if (setjmp(lab) == 0) { /* Yields nothing */
  1651.             do { eprec++; oldd=d; d=d*dbase; }
  1652.             while ((((d+one)-d)-one) == zero && d>oldd);
  1653.             if (d <= oldd) bad=1;
  1654.         } else bad=1;
  1655.     }
  1656.  
  1657.     Unexpected(30);
  1658.  
  1659.     if (bad) {
  1660.       Vprintf("%sCan't determine precision for %s expressions:\n%s%s\n", 
  1661.          co, thing, "   check that you compiled without optimisation!",
  1662.          oc);
  1663.     } else if (eprec==dprec) {
  1664.       Vprintf("%s%s expressions are evaluated in double precision%s\n",
  1665.           co, Thing, oc);
  1666.     } else if (eprec==fprec) {
  1667.       Vprintf("%s%s expressions are evaluated in float precision%s\n",
  1668.           co, Thing, oc);
  1669.     } else if (eprec==lprec) {
  1670.       Vprintf("%s%s expressions are evaluated in long double precision%s\n",
  1671.           co, Thing, oc);
  1672.     } else {
  1673.         Vprintf("%s%s expressions are evaluated in a %s %s %d %s%s\n",
  1674.             co, Thing, eprec>dprec ? "higher" : "lower",
  1675.             "precision than double,\n   using",
  1676.             eprec, "base digits",
  1677.                 oc);
  1678.     }
  1679. }
  1680.  
  1681. #else /* Number */
  1682.  
  1683. #ifdef FPROP
  1684. /* ARGSUSED */
  1685. int FPROP(bits_per_byte) int bits_per_byte; {
  1686.     return 0;
  1687. }
  1688. #endif
  1689. #ifdef EPROP
  1690. /* ARGSUSED */
  1691. Procedure EPROP(fprec, dprec, lprec) int fprec, dprec, lprec; {}
  1692. #endif
  1693.  
  1694. #endif /* ifdef Number */
  1695.  
  1696. #ifdef PASS3
  1697. #undef PASS
  1698. #endif
  1699.  
  1700. #ifdef PASS2
  1701. #undef PASS2
  1702. #define PASS3 1
  1703. #endif
  1704.  
  1705. #ifdef PASS1
  1706. #undef PASS1
  1707. #define PASS2 1
  1708. #endif
  1709.  
  1710. /* If your C compiler doesn't accept the next #include,
  1711.    replace __FILE__ with the file name - and get a new C compiler... */
  1712.  
  1713. #ifdef PASS
  1714. #include __FILE__
  1715. #endif
  1716.  
  1717.